package com.pro.staffmanage.controller;

import com.pro.staffmanage.mapper.DepartmentMapper;
import com.pro.staffmanage.pojo.Department;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @Author: Haotian Cui
 * @Date: 2022/7/30 - 07 - 30 - 16:50
 * @Description: com.pro.staffmanage.controller
 * @Project: staffmanage
 * @version: 1.0
 */
@Controller
@RequestMapping("/dep")
public class DepartmentController {

    @Autowired
    private DepartmentMapper departmentMapper;

    @GetMapping("/all")
    public String getDepartmentList(HttpSession session){
        session.setAttribute("dlist", departmentMapper.queryAllDepartment());
        return "redirect:/deplist";
    }

    @GetMapping("/query/{id}")
    public String queryDepartmentById(@PathVariable("id") Integer id, Model model){
        model.addAttribute("upDep", departmentMapper.queryDepartmentById(id));
        return "dep/list";
    }

    @PostMapping("/add")
    public String addDepartment(Department department){
        departmentMapper.addDepartment(department);
        return "dep/list";
    }

    @PutMapping("/update")
    public String updateDepartment(Department department){
        departmentMapper.updateDepartment(department);
        return "dep/list";
    }

    @DeleteMapping("/delete/{id}")
    public String deleteDepartment(@PathVariable("id") Integer id){
        departmentMapper.deleteDepartment(id);
        return "dep/list";
    }

}
